import mock from '@/mock'
import data from './data/data'
import select from './data/select'
export default {
  'post|/zzxt/api/postUsersInfo': option => {
    const body = JSON.parse(option.body)
    const formdata = body.params.FormData
    data.db_usersinfo_zhiwei.push(formdata)
    data.db_usersinfo_dxz.push(formdata)
    data.db_usersinfo_all.push(formdata)
    return mock.toResult(null)
  },
  'post|/zzxt/api/deleteUsersInfo': option => {
    const body = JSON.parse(option.body)
    const name = body.params.name
    var ArrayId = GetArrayId(name, data.db_usersinfo_all)
    data.db_usersinfo_all.splice(ArrayId, 1)
    ArrayId = GetArrayId(name, data.db_usersinfo_dxz)
    data.db_usersinfo_dxz.splice(ArrayId, 1)
    ArrayId = GetArrayId(name, data.db_usersinfo_zhiwei)
    data.db_usersinfo_zhiwei.splice(ArrayId, 1)
    return mock.toResult(null)
  },
  'put|/zzxt/api/updateUsersInfo': option => {
    const body = JSON.parse(option.body)
    const formdata = body.params.FormData
    const ArrayId = formdata.id
    data.db_usersinfo_all[ArrayId] = formdata
    data.db_usersinfo_dxz[ArrayId] = formdata
    data.db_usersinfo_zhiwei[ArrayId] = formdata
    return mock.toResult(null)
  },
  'get|/zzxt/api/SelectZzmm': option => {
    const result = select.db_zzmm
    return mock.toResult(result)
  },
  'get|/zzxt/api/getGroupNum': option => {
    const result = data.db_GroupNum
    return mock.toResult(result)
  },
  'get|/zzxt/api/getLaborArray': option => {
    const result = select.db_laborArray
    return mock.toResult(result)
  },
  'get|/zzxt/api/getUsersList': option => {
    const result = select.db_userslist
    return mock.toResult(result)
  },
  'post|/zzxt/api/getUsersInfo': option => {
    const body = JSON.parse(option.body)
    const queryObject = body.params.queryObject
    const { page } = queryObject
    const condition = queryObject.condition
    const value = queryObject.value
    // console.log(queryObject)
    if (condition === []) {
      const result = mock.splitPage(Object.assign({ list: data.db_usersinfo_dxz }, page))
      return mock.toResult(result)
    }
    switch (condition[0]) {
      case 'groupnum': {
        const result = mock.splitPage(Object.assign({ list: data.db_usersinfo_dxz }, page))
        return mock.toResult(result)
      }
      case 'labor': {
        const result = mock.splitPage(Object.assign({ list: data.db_usersinfo_zhiwei }, page))
        return mock.toResult(result)
      }
      case 'all': {
        const result = mock.splitPage(Object.assign({ list: data.db_usersinfo_all }, page))
        return mock.toResult(result)
      }
      case 'category': {
        if (value[0] === 'fzdx') {
          const result = mock.splitPage(Object.assign({ list: data.db_usersinfo_fzdx }, page))
          return mock.toResult(result)
        } else if (value[0] === 'jjfz') {
          const result = mock.splitPage(Object.assign({ list: data.db_usersinfo_jjfz }, page))
          return mock.toResult(result)
        } else {
          const result = mock.splitPage(Object.assign({ list: data.db_usersinfo_ybdy }, page))
          return mock.toResult(result)
        }
      }
    }
  }
}

function GetArrayId(name, db_data) {
  const length = db_data.length
  for (var i = 0; i < length; i++) {
    if (db_data[i].name === name) { return parseInt(i) }
  }
  return parseInt('-1')
}
